package com.push_label.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import com.github.pagehelper.PageInterceptor;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

@Configuration
@MapperScan("com.push_label.mapper")
public class MyBatisConfig {

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);

        // 设置Mapper XML文件路径
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath*:mapper/**/*.xml");
        factoryBean.setMapperLocations(resources);

        // 设置实体类别名包路径
        factoryBean.setTypeAliasesPackage("com.push_label.domain");

        // 获取MyBatis配置并设置相关属性
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(false); // 关闭下划线转驼峰
        configuration.setCacheEnabled(false); // 关闭缓存

        factoryBean.setConfiguration(configuration);

        return factoryBean.getObject();
    }


    @Bean
    public PageInterceptor pageInterceptor() {
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = new Properties();

        // 设置数据库方言为PostgreSQL
        properties.setProperty("helperDialect", "postgresql");

        // 设置分页参数合理化，当参数不合理时自动处理
        properties.setProperty("reasonable", "true");

        // 支持通过Mapper接口参数来传递分页参数
        properties.setProperty("supportMethodsArguments", "true");

        // 设置用于Count查询的参数名
        properties.setProperty("params", "count=countSql");

        pageInterceptor.setProperties(properties);
        return pageInterceptor;
    }


    @Bean
    public NestLoopControlInterceptor nestLoopControlInterceptor() throws Exception {
        NestLoopControlInterceptor interceptor = new NestLoopControlInterceptor();
        interceptor.init();
        return interceptor;
    }

    @Bean
    public ApplicationRunner registerInterceptorConfiguration(SqlSessionFactory sqlSessionFactory){
        return args -> {
            org.apache.ibatis.session.Configuration config = sqlSessionFactory.getConfiguration();
            if (!config.getInterceptors().contains(nestLoopControlInterceptor())){
                config.addInterceptor(nestLoopControlInterceptor());
                System.out.println("NestLoopControlInterceptor 已注册");
            }
        };
    }



}
